package dataprocess;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import dao.JdbcDao;
import utils.Config;

public class DataProcessor {

    private static final Logger logger = LogManager.getLogger(DataProcessor.class);

    public static void iueUAVMonitorDataProcess(String data, Config config) {
        try {
            ///*data=ID0123456789ABCDEF,TM2016-01-25 07:59:28,TP00082,HM00072,PM00106,GJE11803.53609,GWN2436.64885,GH1000.1,NP,SP,CO,OT,$*/
            String[] strs = data.split(",");
            IUEUavMonitor uavData = new IUEUavMonitor();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            uavData.setSid(strs[0].substring(2));//strs[0],id
            uavData.setCtime(LocalDateTime.parse(strs[1].substring(2), formatter));//strs[1],日期
            /*
             * if(strs[1]!=null && !strs[1].isEmpty())
             * uavData.setCtime(LocalDateTime.parse(strs[1].substring(2),
             * formatter)); uavData.setStime(LocalDateTime.now());
             */
            
            if (checkStr(strs,2,2))//strs[2],TP
                uavData.setTmp(Double.parseDouble(strs[2].substring(2)));
            if (checkStr(strs,3,2))//strs[3],HM
                uavData.setHum(Integer.parseInt(strs[3].substring(2)));
            if (checkStr(strs,4,2))//strs[4],PM
                uavData.setPm25(Integer.parseInt(strs[4].substring(2)));
            if (checkStr(strs,5,3))//strs[5],GJE精度
                uavData.setLng(Double.parseDouble(strs[5].substring(3)));
            if (checkStr(strs,6,3))//strs[6],GWN维度
                uavData.setLat(Double.parseDouble(strs[6].substring(3)));
            if (checkStr(strs,7,3))//strs[7],GH高度
                uavData.setAsl(Double.parseDouble(strs[7].substring(2)));
            if (checkStr(strs,8,2))//strs[8],NP
                uavData.setNp(Double.parseDouble(strs[8].substring(2)));
            if (checkStr(strs,9,2))//strs[9],SP
                uavData.setSp(Double.parseDouble(strs[9].substring(2)));
            if (checkStr(strs,10,2))//strs[10],CO
                uavData.setCo(Double.parseDouble(strs[10].substring(2)));
            if (checkStr(strs,11,2))//strs[11],OT
                uavData.setOt(Double.parseDouble(strs[11].substring(2)));
            JdbcDao.insertUAVMonitorData(uavData, config);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("Parsing the IueUAVMonitor data:[" + data + "], exception=" + e);
        }
    }

    private static boolean checkStr(String[] strs,int i, int j) {
        return strs[i] != null && !strs[i].isEmpty() && !strs[i].substring(j).isEmpty();
    }

    public static void iueWindDataProcess(String data, Config config) {
        try {
            String[] strs = data.split(",");
            IUEWind wind = new IUEWind();
            
            if (checkStr(strs,1,1))
                wind.setWdir(Integer.parseInt(strs[1]));
            if (checkStr(strs,2,2))
                wind.setWspd(Double.parseDouble(strs[2]));
            //无需插入sid，因为sid由sql语句中指定
            //无需插入时间，因为时间由sql的getDate获得
            
            JdbcDao.insertIUEWindData(wind, config);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("Parsing the IUEWind data:[" + data + "], exception=" + e);
        }
    }
    
    public static void iueSuperStationProcess(String data, Config config){
        IUESuperStation iss = new IUESuperStation();
        try {
            String rec[] = data.split(";");
            String timeStamp = rec[4].split("&&")[1].split("=")[1];
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
            LocalDateTime dateTime = LocalDateTime.parse(timeStamp, formatter);
            iss.setTimestamp(dateTime);
            for (int i = 5; i < rec.length; i++) {
                String partData = rec[i].split(",")[0];//数据
                String partComm = rec[i].split(",")[1];//通讯状态
                if (partData!=null){
                    String code = partData.split("-")[0]; //获取字段编码
                    String commStat = partComm.split("=")[1];
                    Double value = Double.parseDouble(partData.split("=")[1]);
                    if(commStat!=null && !commStat.equals("-1")){//通讯正常
                        setPropByCode(iss, code, value);
                    }else{//通讯不正常,设置为null
                        setPropByCode(iss, code, null);
                    }
                }
            }
            JdbcDao.insertIueSuperStation(iss, config);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
    }

    private static void setPropByCode(IUESuperStation iss, String code, Double value) {
        String field = SuperStationDict.get(code); //根据编码获得字段名
        if(field!=null){
            switch (field) {
            case "SO2":
                iss.setSo2(value);
                break;
            case "NO2":
                iss.setNo2(value);
                break;
            case "PM10":
                iss.setPm10(value);
                break;
            case "PM25":
                iss.setPm25(value);
                break;
            case "CO":
                iss.setCo(value);
                break;
            case "O3":
                iss.setO3(value);
                break;
            case "WINS":
                iss.setWins(value);
                break;
            case "WIND":
                iss.setWind(value);
                break;
            case "TEMPR":
                iss.setTempr(value);
                break;
            case "HUM":
                iss.setHum(value);
                break;
            case "PRESS":
                iss.setPress(value);
                break;
            case "CLEAR":
                iss.setClear(value);
                break;
            default:
                break;
            }
        }
    }

}
